<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-17
 * 版    本：1.0.0
 * 功能说明：后台登录控制器。
 *
 **/

namespace Qwadmin\Controller;

use Qwadmin\Controller\ComController;

class LoginController extends ComController
{
    public function index()
    {
        $flag = $this->check_login();
        if ($flag) {
            $this->error('您已经登录,正在跳转到主页', U(C('HomeLink')));
        }

        $this->display();
    }

    public function login()
    {
        $verify = isset($_POST['verify']) ? trim($_POST['verify']) : '';
        /*if (!$this->check_verify($verify, 'login')) {
           $this->error('验证码错误！', U("login/index"));
        }*/

        $username = htmlspecialchars(I("user" , '')  , ENT_NOQUOTES);
        $password = I("password" , '');
        $remember = I("remember" , 0);
        if ($username == '') {
            $this->error('用户名不能为空！', U("login/index"));
        } elseif ($password == '') {
            $this->error('密码必须！', U("login/index"));
        }

        $model = M("Member");
        $user = $model->field('uid,user,password,code,is_lock,level')->where(array('user' => $username))->find();
        
        if ($user) {
            $code = $user['code'];
            $auth = password($password , $code) ;
            
            if($user['password'] != $auth){     //密码错误 
                addlog('密码错误。', $username);
                $this->error('密码错误，请重试！', U("login/index"));die;
            }
            if($user['is_lock'] != 1){
				$this->error('帐号被禁用，请联系管理员！', U("login/index"));die;
			}
            $ip = get_client_ip();
            $ua = $_SERVER['HTTP_USER_AGENT'];
            session_start();
            session('uid',$user['uid']);
            addlog('登录成功。');
			$this->USER = $user;
			$this->setHomeLink();//获取登录后的跳转地址
            $url = U(C('HomeLink'));
            header("Location: $url");
            exit(0);
        } else {
            addlog('用户不存在。', $username);
            $this->error('用户不存在！', U("login/index"));
        }
    }

    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    public function verify()
    {
        $config = array(
            'fontSize' => 14, // 验证码字体大小
            'length' => 4, // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
            'imageW' => 100,
            'imageH' => 30,
        );
        $verify = new \Think\Verify($config);
        $verify->entry('login');
    }
}
